//
//  FRQForgetDeviceViewController.m
//  FRQBluetoothKit_Example
//
//  Created by admin on 2023/10/23.
//  Copyright © 2023 xuchunhai. All rights reserved.
//

#import "FRQForgetDeviceViewController.h"
#import "WelcomeBackViews.h"
#import "ForgetDeviceViews.h"
#import "FRQViewController.h"
#import "UILabel+YBAttributeTextTapAction.h"
#import "FRQDevicePairViewController.h"
#import "FRQLoginViewController.h"
@interface FRQForgetDeviceViewController ()
@property (nonatomic,strong) ForgetDeviceViews *ForgetDeviceView;
@property (nonatomic,strong) WelcomeBackViews *WelcomeBackView;
@end

@implementation FRQForgetDeviceViewController
-(void)appDeviceDelete{
    NSDictionary *dic=@{@"devNo":[NSString stringWithFormat:@"%@",skDevice.devNo],
                        @"devName":[NSString stringWithFormat:@"%@",skDevice.devName],
                        @"mac":@"",
                        @"uuid":[NSString stringWithFormat:@"%@",skDevice.uuid]
    };
    
    
    [skNetTools skPOSTWithURL:[NSString stringWithFormat:@"%@%@",skURL,@"app/device/delete"] parameters:dic cachePolicy:0 callback:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"数据=%@",responseObject);
        skNetModel *model=responseObject;
        if(model.code==200){
            [[FRQBluetoothManager shareManager] closeAllPeripheralsConnection:^(id<FRBleAbility>  _Nonnull ability) {
            }];
            self.WelcomeBackView.hidden=NO;
            self.ForgetDeviceView.hidden=YES;
        }else{
            [SkToast SkToastShow:model.msg];
        }
    }];
}
- (ForgetDeviceViews *)ForgetDeviceView{
    if (nil==_ForgetDeviceView) {
        _ForgetDeviceView=skXibView(@"ForgetDeviceViews");
        [self.view addSubview:_ForgetDeviceView];
        [_ForgetDeviceView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuide);
            make.bottom.mas_equalTo(self.mas_bottomLayoutGuide);
            make.right.left.mas_equalTo(0);
        }];
        @weakify(self)
        [[_ForgetDeviceView.btnBack rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }];
        [[_ForgetDeviceView.btnYes rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self appDeviceDelete];
        }];
        [[_ForgetDeviceView.btnNo rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }];
    }
    return _ForgetDeviceView;
}
- (WelcomeBackViews *)WelcomeBackView{
    if (nil==_WelcomeBackView) {
        _WelcomeBackView=skXibView(@"WelcomeBackViews");
        [self.view addSubview:_WelcomeBackView];
        [_WelcomeBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuide);
            make.bottom.mas_equalTo(self.mas_bottomLayoutGuide);
            make.right.left.mas_equalTo(0);
        }];
        @weakify(self)
        [[_WelcomeBackView.btnPairDevice rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            if(self.viewType==1){
                FRQViewController *view=[[FRQViewController alloc] init];
                view.typeShow=0;
                view.modalPresentationStyle=0;
                [self presentViewController:view animated:YES completion:^{
                    
                }];
            }else{
                [self dismissViewControllerAnimated:YES completion:^{
                    if(self.bvalCount == 1){
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"PairBack" object:nil];
                    }
                }];
            }
        }];
        [[_WelcomeBackView.btnKefu rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)

            [self dismissViewControllerAnimated:YES completion:^{
                if(self.bvalCount == 1){
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"KefuBack" object:nil];
                }
            }];
        }];
        [_WelcomeBackView.labTip yb_addAttributeTapActionWithStrings:@[@"Log Out"] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
            @strongify(self);
            [self clearAllUserDefaultsData2];
            UIViewController *view=[[FRQLoginViewController alloc] init];
            UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:view];
            [[[UIApplication sharedApplication] delegate] window].rootViewController=nav;
        }];
    }
    return _WelcomeBackView;
}
/// 方法二：清除持久域
-(void)clearAllUserDefaultsData2{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSDictionary *dic = [userDefaults dictionaryRepresentation];
    
    for(id key in dic){
        [userDefaults removeObjectForKey:key];
    }
    [userDefaults synchronize];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self ForgetDeviceView];
    [self WelcomeBackView];
    if (self.viewType==0) {//忘记设备
        _WelcomeBackView.hidden=YES;
        _ForgetDeviceView.hidden=NO;
    }else{//欢迎链接设备
        _WelcomeBackView.hidden=NO;
        _ForgetDeviceView.hidden=YES;
    }
    
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
